﻿'Option Strict On
Option Explicit On
Imports System
Imports System.IO
Imports System.Xml
Imports System.Data
Imports System.Windows.Forms.DataVisualization.Charting
Imports System.Net.Mime.MediaTypeNames
Imports System.Reflection ' For Missing.Value and BindingFlags
Imports System.Runtime.InteropServices ' For COMException
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Drawing.Imaging
Imports Analyzer.MainForm


Public Class Charts
    Dim index As Integer
    'Dim xmlFile As XmlReader
    'Dim ds As New DataSet
    Dim chartSaveFlag As Boolean


    Private Sub Chart_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'xmlFile = XmlReader.Create("C:\Users\lalala\Desktop\tzanet.xml", New XmlReaderSettings())
        'ds.ReadXml(xmlFile)
        'MainForm.mdsLogFile
        Dim i As Integer
        If (MainForm.mdsLogFile.Tables.Count <> 0) Then

            For i = 0 To MainForm.mdsLogFile.Tables(0).Columns.Count - 1
                cbAtrubute.Items.Add(MainForm.mdsLogFile.Tables(0).Columns(i).ToString)
                'MsgBox(MainForm.mdsLogFile.Tables(0).Columns(i).ToString) tha mas emfanisei to date time action
            Next
        End If
    End Sub

    Private Sub cbAtrubute_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbAtrubute.SelectedIndexChanged
        lbAnalyzer.Items.Clear()

        Dim j As Integer

        Dim SelectItem As String = cbAtrubute.SelectedItem.ToString()

        Dim NuberOfAtribute As Integer = MainForm.mdsLogFile.Tables(0).Columns.Count

        For j = 0 To MainForm.mdsLogFile.Tables(0).Columns.Count - 1
            If SelectItem.Equals(MainForm.mdsLogFile.Tables(0).Columns(j).ToString) Then
                index = j
            End If
        Next

        For i = 0 To MainForm.mdsLogFile.Tables(0).Rows.Count - 1
            lbAnalyzer.Items.Add(MainForm.mdsLogFile.Tables(0).Rows(i).Item(index).ToString)
        Next

    End Sub

   Private Sub btnShowChart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowChart.Click
        ch_Chart.Titles.Clear()


      Dim list As List(Of String) = New List(Of String)
        Dim s As New Series
        list.Clear()

      If (MainForm.mdsLogFile.Tables.Count = 0) Then
         MsgBox("Please insert data")
        Else

            If chbAnalizing.Checked Then
                For Each SelectItems In lbAnalyzer.SelectedItems
                    If Not list.Contains(SelectItems) Then
                        list.Add(SelectItems.ToString)
                    End If
                Next
                '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
            Else

                For i = 0 To MainForm.mdsLogFile.Tables(0).Rows.Count - 1
                    'MsgBox(MainForm.mdsLogFile.Tables(0).Rows(i).Item(index).ToString) ta emfanizei ola
                    If Not list.Contains(MainForm.mdsLogFile.Tables(0).Rows(i).Item(index).ToString) Then
                        list.Add(MainForm.mdsLogFile.Tables(0).Rows(i).Item(index).ToString)
                    End If
                Next
            End If
            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

            If cbAtrubute.Text <> Nothing Then
                pbNothingSelected.Visible = False

                If (list.Count > 12) Then
                    Dim response As MsgBoxResult

                    response = MsgBox("Too many values to display. (" & CStr(list.Count) & ")" & vbCrLf & "Do not expect an easily readable chart.", MsgBoxStyle.DefaultButton1)
                End If

                With Me.ch_Chart
                    .Series.Clear()
                    With .Series.Add("")
                        s.Name = cbAtrubute.SelectedItem.ToString()
                        'Morfopoisi tou titlou sta diagrammata
                        Call ChartTitle()

                        ' kalo tin methodo RuddionButtonCase
                        Call RuddionButtonCase()

                        For Each strX As String In list
                            Dim counterPie As Integer = 0
                            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
                            If chbAnalizing.Checked Then
                                For Each SelectItems In lbAnalyzer.SelectedItems
                                    If SelectItems.ToString = strX Then
                                        counterPie += 1
                                    End If

                                Next

                            Else
                                For m = 0 To MainForm.mdsLogFile.Tables(0).Rows.Count - 1
                                    If MainForm.mdsLogFile.Tables(0).Rows(m).Item(index).ToString = strX Then
                                        counterPie += 1
                                    End If
                                Next

                            End If
                            '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

                            .Points.AddXY(strX, counterPie)
                        Next
                        .IsVisibleInLegend = False
                        .ToolTip = "#VALX"


                        If (rbtnPercent.Checked) Then
                            .Label = "#VALX \n #PERCENT"
                        Else
                            .Label = "#VALX \n (#VALY)"
                        End If

                    End With

                    'Kalo tin methodo 3D style
                    Call Style3D()

                End With
                'Kalo tin methodo ColumnsColor
                Call ColumnsColor()

                chartSaveFlag = True
            Else
                pbNothingSelected.Visible = True
            End If
        End If

    End Sub
    Private Sub chbFill_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbFill.CheckedChanged
        If (chbFill.Checked) Then
            ch_Chart.Dock = DockStyle.Fill
            lblTitlos.Visible = False
            lblSubbtitle.Visible = False
            txtTitle.Visible = False
            txtSubbtitle.Visible = False
            gbColumnsColor.Visible = False
            btnPrintChart.Visible = False
            lbAnalyzer.Visible = False
            chbAnalizing.Visible = False
        End If
    End Sub
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Dim response As MsgBoxResult
        response = MsgBox("Do you want to close form?", MsgBoxStyle.YesNo, "Confirm")
        If response = MsgBoxResult.Yes Then
            Me.Dispose()
            MainForm.mdsLogFile.Dispose()
        ElseIf response = MsgBoxResult.No Then
            Exit Sub
        End If
    End Sub
    Private Sub ch_Chart_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ch_Chart.DoubleClick
        ch_Chart.Dock = DockStyle.None
        lblTitlos.Visible = True
        lblSubbtitle.Visible = True
        txtTitle.Visible = True
        txtSubbtitle.Visible = True
        gbColumnsColor.Visible = True
        btnPrintChart.Visible = True
        'lbAnalyzer.Visible = True
        chbAnalizing.Visible = True
    End Sub

    Private Sub btnSaveChart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveChart.Click

        Dim myStream As Stream
        Dim saveFileDialog1 As New SaveFileDialog()

        If (chartSaveFlag = True) Then
            saveFileDialog1.Filter = "Jpeg files (*.Jpeg)|*.Jpeg|All Jpeg (*.*)|*.*"
            saveFileDialog1.FilterIndex = 2
            saveFileDialog1.RestoreDirectory = True

            If saveFileDialog1.ShowDialog() = DialogResult.OK Then
                myStream = saveFileDialog1.OpenFile()
                ch_Chart.SaveImage(myStream, ChartImageFormat.Jpeg)
                If (myStream IsNot Nothing) Then
                    MsgBox("Chart was save successfully")
                    myStream.Close()
                End If
            End If

        Else
            MsgBox("Παρακαλώ εμφανίστε ένα διάγραμμα πρώτα ")
        End If
    End Sub

    Private Sub RuddionButtonCase()
        If (rbtnBar.Checked) Then
            ch_Chart.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Column
        ElseIf (rbtnPie.Checked) Then
            ch_Chart.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Pie
        ElseIf (rbtnLine.Checked) Then
            ch_Chart.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
        ElseIf (rbtnArea.Checked) Then
            ch_Chart.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Area
        ElseIf (rbtnPyramid.Checked) Then
            ch_Chart.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Pyramid
        ElseIf (rbtnCandlestick.Checked) Then
            ch_Chart.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Candlestick
        End If
    End Sub

    Private Sub ChartTitle()
        Dim T As Title
        If (txtTitle.Text.Length = 0) Then
            If (txtSubbtitle.Text.Length = 0) Then
                T = ch_Chart.Titles.Add("Title: " + cbAtrubute.SelectedItem.ToString() + "  Subbtitle: --")
            Else
                T = ch_Chart.Titles.Add("Title :" + cbAtrubute.SelectedItem.ToString() + "  Subbtitle: " + txtSubbtitle.Text.ToString)
            End If
        Else
            If (txtSubbtitle.Text.Length = 0) Then
                T = ch_Chart.Titles.Add("Title :" + txtTitle.Text + "  Subbtitle: --")
            Else
                T = ch_Chart.Titles.Add("Title :" + txtTitle.Text + "  Subbtitle: " + txtSubbtitle.Text.ToString)
            End If
        End If

        With T
            .ForeColor = Color.Black
            .BackColor = Color.AliceBlue

            '~~> Setting Font, Font Size and Bold/Italicizing
            .Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Bold)
            .Font = New System.Drawing.Font("Times New Roman", 11.0F, System.Drawing.FontStyle.Underline)
            .BorderColor = Color.Black
            .BorderDashStyle = ChartDashStyle.DashDotDot
        End With
    End Sub

    Private Sub ColumnsColor()
        If (rbtnBlue.Checked) Then
            ch_Chart.Palette = ChartColorPalette.BrightPastel
        ElseIf (rbtnGrey.Checked) Then
            ch_Chart.Palette = ChartColorPalette.Grayscale
        ElseIf (rbtnOrange.Checked) Then
            ch_Chart.Palette = ChartColorPalette.EarthTones
        ElseIf (rbtnGreen.Checked) Then
            ch_Chart.Palette = ChartColorPalette.Bright
        ElseIf (rbtnDefaultColor.Checked) Then
            ch_Chart.Palette = ChartColorPalette.SemiTransparent
        End If
    End Sub
    Private Sub Style3D()
        If (chb_3DStyle.Checked) Then

            ch_Chart.ChartAreas(0).Area3DStyle.Enable3D = True
            ch_Chart.Series(0)("PieLabelStyle") = "Outside"
        Else
            ch_Chart.ChartAreas(0).Area3DStyle.Enable3D = False
        End If
    End Sub

    Private Sub btnPrintChart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintChart.Click
        If (MainForm.mdsLogFile.Tables.Count = 0) Then
            MsgBox("Please insert data")
        Else
            ch_Chart.Printing.Print(True)
        End If
    End Sub

    Private Sub chbAnalizing_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chbAnalizing.CheckedChanged
        If chbAnalizing.Checked Then
            lbAnalyzer.Visible = True
        Else
            lbAnalyzer.Visible = False
        End If
    End Sub
End Class

